package com.thememaker.lockstyle;

import com.alibaba.fastjson.JSON;
import com.thememake.Constants;
import com.thememake.common.CommonApp;
import com.thememake.common.Context;
import com.thememake.entity.ThemeFileInfo;
import com.thememake.entity.User;
import com.thememake.ninepatch.OSUtil;
import com.thememake.ui.R;
import com.thememake.ui.Toast;
import com.thememake.util.FileUtils;
import com.thememake.util.GsonUtils;
import com.thememaker.util.AutoSaveUtils;
import com.thememaker.util.VersionControlUtils;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * Created by jiajun.jiang on 2018/2/23.
 */
public class LockstyleApp extends CommonApp {
    private ThemeFileInfo themeFileInfo;
    private static String argsString;
    private static String[] openArgs;

    public LockstyleApp(){
        super();
    }

    public LockstyleApp(ThemeFileInfo themeFileInfo){
        super();
        this.themeFileInfo = themeFileInfo;
    }


    public static void main(String[] args) {
        argsString = JSON.toJSONString(args);
        System.err.println("argsString >>> " + argsString);
        openArgs = args;
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        super.start(primaryStage);
        if (themeFileInfo == null) {
            if (openArgs != null && openArgs.length>=3) {
                User user = new User();
                try {
                    System.err.println("openArgs[1] >>> " + openArgs[1]);
                    user = GsonUtils.loadAs(openArgs[2], User.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                themeFileInfo = new ThemeFileInfo(openArgs[1],
                        new File(openArgs[0]),
                        user, false);
            }else{
                   File testTheme = FileUtils.getResFileOfThemeModel(Constants.VIVO,"theme");
                   if (testTheme.exists()){
                       User user = new User();
                       user.setNickname("测试");
                       user.setVipRank(100);
                       themeFileInfo = new ThemeFileInfo(Constants.VIVO, testTheme,
                               user, false);
                   }else {
                       primaryStage.close();
                       Toast.makeText(primaryStage,"非法用户，请联系管理员!",1000);
                   }
            }
        }
        if (!Files.exists(Paths.get(themeFileInfo.getThemeDir().getPath(), themeFileInfo.getThemeType(), ThemeFileInfo.LOCKSTYLE_NAME, "lockscreen"))){
            new LockscreenModelChooseDialog(new Stage(),themeFileInfo).show();
            AutoSaveUtils.getInstance(themeFileInfo).stopSave();
            primaryStage.close();
        }else{
            String title = VersionControlUtils.VERSIONNAME;
            if (VersionControlUtils.DEBUG){
                title += " 测试版";
            }
            primaryStage.setTitle("彩虹糖锁屏制作工具 - " + title);
            primaryStage.getIcons().add(new Image(
                    getClass().getResourceAsStream(Context.getDrawable(R.drawable.launcher))));
            LockstyleMainController mainController = loadPage(R.layout.lockstyle_main_layout);
            mainController.showLockStyleView(primaryStage, themeFileInfo);
            if (OSUtil.getOS() == OSUtil.WINDOWS) {
                primaryStage.setMaximized(true);
            }
            primaryStage.show();
            primaryStage.setOnCloseRequest(event -> {
                AutoSaveUtils.getInstance(themeFileInfo).stopSave();
                primaryStage.close();
            });
            VersionControlUtils.checkVersion();
        }
    }
}
